package com.beaven.model.data.helper;

import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.group.GroupCreateModel;
import com.beaven.model.model.api.group.GroupMemberCreateModel;
import com.beaven.model.model.api.group.UpdateGroupModel;
import com.beaven.model.model.card.GroupCard;
import com.beaven.model.model.card.GroupMemberCard;
import com.beaven.model.model.db.Group;
import com.beaven.model.model.db.Group_Table;
import com.beaven.model.net.HTTPService;
import com.beaven.model.net.NetFactory;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.util.List;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author: Beaven
 * time: 2017/6/1 11:15
 */

public class GroupHelper {

  public static Group findFromLocal(String groupId) {
    return SQLite.select().from(Group.class).where(Group_Table.id.eq(groupId)).querySingle();
  }

  public static Observable<RspModel<GroupCard>> createGroup(GroupCreateModel model) {
    HTTPService service = NetFactory.getService();
    return service.createGroup(model)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<List<GroupCard>>> getGroupList() {
    HTTPService service = NetFactory.getService();
    return service.getGroups()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<GroupCard>> updateGroup(UpdateGroupModel model) {
    HTTPService service = NetFactory.getService();
    return service.updateGroup(model)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<List<GroupMemberCard>>> getGroupMember(String groupId) {
    HTTPService service = NetFactory.getService();
    return service.getGroupMember(groupId).subscribeOn(Schedulers.io());
  }

  public static Observable<RspModel<List<GroupMemberCard>>> addMember(
      List<GroupMemberCreateModel> model) {
    HTTPService service = NetFactory.getService();
    return service.addGroupMember(model)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<List<GroupMemberCard>>> deleteMember(List<GroupMemberCreateModel> model) {
    HTTPService service = NetFactory.getService();
    return service.deleteGroupMember(model)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel> deleteGroup(String groupId) {
    HTTPService service = NetFactory.getService();
    return service.deleteGroup(groupId)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }
}
